﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasketStats.Core.Team;

namespace BasketStats
{
    public partial class GameRosterSelectorForm : Form
    {
        private class PlayerInfoInternal
        {
            public PlayerInfo Player;
            public bool Convocato;
            public override string ToString()
            {
                return string.Format("{0} {1} ({2})", Player.Name, Player.FamilyName, Player.Role);
            }
        }

        internal IEnumerable<PlayerInfo> Selected
        {
            get
            {
                return chkRoster.Items.OfType<PlayerInfoInternal>().Where(pi => pi.Convocato).Select(pi => pi.Player);
            }
        }

        public GameRosterSelectorForm()
        {
            InitializeComponent();
        }

        private Roster m_roster;
        internal Roster Roster {
            get
            {
                return m_roster;
            }
            set
            {
                m_roster = value;
                PopulateList();
            }
        }

        private void PopulateList()
        {
            foreach (var player in Roster.Players)
            {
                chkRoster.Items.Add(new PlayerInfoInternal { Player = player});
            }
            chkRoster.SelectedIndexChanged += SelectedItemChanged;
            chkRoster.ItemCheck += ItemChecked;
        }

        private void SelectedItemChanged(object sender, EventArgs e)
        {
            var selectedItem = ((PlayerInfoInternal)chkRoster.SelectedItem);
            txtPlayerName.Text = selectedItem.Player.Name;
            txtPlayerSurname.Text = selectedItem.Player.FamilyName;
            txtPlayerRole.Text = string.Format("({0})", selectedItem.Player.Role);
        }

        private void ItemChecked(object sender, ItemCheckEventArgs e)
        {
            ((PlayerInfoInternal)chkRoster.Items[e.Index]).Convocato = e.NewValue == CheckState.Checked;
        }

        private void Roster_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
